﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SMP_QuanLySV.DAL;

namespace SMP_QuanLySV.BLL
{
    class BLLTonGiao
    {
        List<TonGiao> list;
        QuanLySVDataContext db;
        

        // constructor
        public BLLTonGiao()
        {
            this.list = new QuanLySVDataContext().TonGiaos.ToList();
            this.db = new QuanLySVDataContext();
        }

        // kiem tra 1 ton giao co ton tai hay khong
        public bool checkExist(TonGiao temp)
        {
            foreach (TonGiao tg in list)
            {
                if (tg.TonGiaoID.Equals(temp.TonGiaoID))
                    return true;
            }
            return false;
        }

        // update thong tin ton giao
        public bool updateTonGiao(TonGiao update)
        {
            try
            {                

                var temp = (from c in db.TonGiaos
                            where c.TonGiaoID == update.TonGiaoID
                            select c).First();
                TonGiao found = (TonGiao)temp;
                found.TonGiaoID = update.TonGiaoID;
                found.TenTonGiao = update.TenTonGiao;
                found.GhiChu = update.GhiChu;

                db.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        // them ton giao moi vao database
        public bool insertTonGiao(TonGiao temp)
        {
            try
            {
                db.TonGiaos.InsertOnSubmit(temp);

                db.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        // xoa ton giao
        public bool deleteTonGiao(TonGiao temp)
        {
            try
            {
                var dt = (from c in db.TonGiaos
                          where c.TonGiaoID == temp.TonGiaoID
                          select c).First();
                TonGiao found = (TonGiao)dt;

                db.TonGiaos.DeleteOnSubmit(found);

                db.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        // tra ve danh sach cac ton giao
        public List<TonGiao> returnListTonGiao()
        {
            this.list = new QuanLySVDataContext().TonGiaos.ToList();
            return list;
        }

        // tra ve 1 ton giao theo so thu tu trong list
        public TonGiao retrieveByIndex(int i)
        {
            this.list = new QuanLySVDataContext().TonGiaos.ToList();
            if (i >= 0 && i < list.Count)
                return list[i];
            else return null;
        }

        // tra ve 1 ton giao theo ma ton giao
        public TonGiao retrieveByID(string ID)
        {
            this.list = new QuanLySVDataContext().TonGiaos.ToList();
            foreach (TonGiao temp in list)
                if (temp.TonGiaoID == ID)
                {
                    return temp;
                }
            return null;
        }

        // kiem tra thong tin 1 ton giao co hop le hay khong
        public bool kiemTra(TonGiao temp)
        {
            if (temp.TonGiaoID == "") return false;
            return true;
        }
    }
}
